package com.jzg.jcptadmin.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.app.AppManager;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.data.vo.TaskViewData;
import com.jzg.jcptadmin.presenter.TaskViewPresenter;
import com.jzg.jcptadmin.viewinterface.TaskViewInterface;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jzg on 2016/1/14.
 * 操作中审核界面
 */
public class ExamineActivity extends BaseActivity implements TaskViewInterface {
    @Bind(R.id.input_top_text)
    TextView inputTopText;
    @Bind(R.id.vin_submit)
    RelativeLayout vinSubmit;
    @Bind(R.id.act_vin_top_layout)
    RelativeLayout actVinTopLayout;
    @Bind(R.id.detection_result_edit)
    EditText detectionResultEdit;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.rb_button1)
    RadioButton rbButton1;
    @Bind(R.id.rb_button2)
    RadioButton rbButton2;
    @Bind(R.id.rg_radioGroup)
    RadioGroup rgRadioGroup;
    @Bind(R.id.rl_rela)
    RelativeLayout rlRela;

    private String Status = "";
    private String result = "";
    private String id = "";
    private String type = "";
    private TaskViewPresenter taskViewPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_examine);
        ButterKnife.bind(this);
        taskViewPresenter = new TaskViewPresenter(DataManager.getInstance());
        taskViewPresenter.attachView(this);
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        rgRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_button1:
                       /* detectionResultEdit.setFocusable(false);
                        detectionResultEdit.setFocusableInTouchMode(false);
                        detectionResultEdit.setHint("");*/
                        rlRela.setVisibility(View.GONE);
                        break;
                    case R.id.rb_button2:
                    /*    detectionResultEdit.setFocusable(true);
                        detectionResultEdit.setFocusableInTouchMode(true);
                        detectionResultEdit.setHint("填写回复消息");*/
                        rlRela.setVisibility(View.VISIBLE);
                        break;
                    default:

                        break;
                }
            }
        });
        initView();
    }

    private void initView() {
        detectionResultEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                count.setText(s.length() + "/200");
            }
        });

    }

    public void input_submit(View view) {
        if (rbButton1.isChecked()) {
            Status = "5";

        } else if (rbButton2.isChecked()) {
            Status = "1";
            result = detectionResultEdit.getText().toString().trim();
            if (TextUtils.isEmpty(result)) {
                Toast.makeText(ExamineActivity.this, "驳回原因不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

        }else if(!rbButton1.isChecked()&&!rbButton2.isChecked()){
            Toast.makeText(ExamineActivity.this, "请选择审核类型", Toast.LENGTH_SHORT).show();
            return;
        }

        submitData();//提交数据
    }

    private void submitData() {
        showDialog();
        taskViewPresenter.ZyOperationData(appContext.getUser().getUserId() + "", type, id, result, Status);
    }

    @Override
    public void showViewDatas(TaskViewData datas) {
        dismissDialog();
    }

   /* //提交返回的结果
    @Override
    public void DeleteDataStadus(SuccessData datas) {
    }*/

    @Override
    public void OperationDatas(SuccessData datas) {
        dismissDialog();
        if (datas.getStatus() == 100) {
            AppManager.getAppManager().finishActivity(TaskViewActivity.class);
            AppManager.getAppManager().finishActivity(OfflineDetectionActivity.class);
            Intent intent = new Intent(ExamineActivity.this, OfflineDetectionActivity.class);
            startActivity(intent);
            Toast.makeText(ExamineActivity.this, "审核成功", Toast.LENGTH_SHORT).show();
            finish();

        } else {
            Toast.makeText(ExamineActivity.this,datas.getMsg() + "", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showError(String error) {
        dismissDialog();
        Toast.makeText(ExamineActivity.this, Constant.errr_net, Toast.LENGTH_SHORT).show();
    }


}
